package ch.zbw.pra.sysventory.presentation.panels;

import ch.zbw.pra.sysventory.presentation.main.Main;

import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

/**
 * Painted das LoginPanel
 * 
 * @author Ralph Koster
 */

public class LoginPanel extends VerticalLayout implements Button.ClickListener {

	private static final long serialVersionUID = -7511461271265777458L;
	private Main main;
	private CssLayout loginPanel;
	private HorizontalLayout labelsPanel;
	private HorizontalLayout fieldsPanel;
	private HorizontalLayout errorPanel;
	private Label welcome;
	private Label title;
	private Label errorIcon;
	private Label errorText;
	private TextField username;
	private PasswordField password;
	private Button loginButton;
	
	private boolean wrongLoginData;
	
	public LoginPanel(Main main) {
		this.main = main;
		this.loginPanel = new CssLayout();
		this.labelsPanel = new HorizontalLayout();
		this.fieldsPanel = new HorizontalLayout();
		this.errorPanel = new HorizontalLayout();
		this.welcome = new Label("Willkommen");
		this.title = new Label("ZbW Sysventory");
		this.errorIcon = new Label();
		this.errorText = new Label();
		this.username = new TextField("Benutzername");
		this.password = new PasswordField("Passwort");
		this.loginButton = new Button("Login");
		
		this.wrongLoginData = false;
		
		paint();
	}
	
	public void paint() {
		this.setSizeFull();
        this.addStyleName("login-layout");

        loginPanel.addStyleName("login-panel");  
        
        labelsPanel.setMargin(true);
        labelsPanel.addStyleName("labels-panel");

        welcome.setSizeUndefined();
        welcome.addStyleName("h4");

        title.setSizeUndefined();
        title.addStyleName("h2");
        
        labelsPanel.addComponent(welcome);
        labelsPanel.setComponentAlignment(welcome, Alignment.MIDDLE_LEFT);
        labelsPanel.addComponent(title);
        labelsPanel.setComponentAlignment(title, Alignment.MIDDLE_RIGHT);

        fieldsPanel.setSpacing(true);
        fieldsPanel.setMargin(true);
        fieldsPanel.addStyleName("fields");

        username.focus();
        loginButton.addStyleName("default");
        loginButton.setClickShortcut(KeyCode.ENTER);
        loginButton.addListener(this);
        
        fieldsPanel.addComponent(username);
        fieldsPanel.addComponent(password);        
        fieldsPanel.addComponent(loginButton);
        fieldsPanel.setComponentAlignment(loginButton, Alignment.BOTTOM_LEFT);

        loginPanel.addComponent(labelsPanel);
        loginPanel.addComponent(fieldsPanel);
        
        if(wrongLoginData) {
        	paintErrorPanel();
        } else {
        	username.setStyleName("default");
        	password.setStyleName("default");
        	loginPanel.removeComponent(errorPanel);
        }

        this.addComponent(loginPanel);
        this.setComponentAlignment(loginPanel, Alignment.MIDDLE_CENTER);
	}
	
	public void paintErrorPanel() {
		errorIcon.setIcon(new ThemeResource("img/arrow.png"));
    	errorIcon.setSizeFull();
    	errorIcon.setStyleName("error-icon");
    	errorText.setValue("Falscher Benutzername oder falsches Passwort!!!");
        errorText.addStyleName("error-h4");
    	errorPanel.addComponent(errorIcon);
    	errorPanel.setComponentAlignment(errorIcon, Alignment.MIDDLE_LEFT);
    	errorPanel.addComponent(errorText);
    	errorPanel.setComponentAlignment(errorText, Alignment.MIDDLE_RIGHT);
    	errorPanel.setStyleName("error-panel");
    	username.setStyleName("username-error");
    	password.setStyleName("password-error");
    	loginPanel.addComponent(errorPanel);
	}

	@Override
	public void buttonClick(ClickEvent event) {
		delegateLogin();
		username.setValue("");
		password.setValue("");
	}
	
	private void delegateLogin() {
		String usernameValue = username.getValue().toString();
		String passwordValue = new String(password.getValue().toString());
		if(usernameValue.isEmpty() 
				|| passwordValue.isEmpty()
				|| usernameValue == null
				|| passwordValue == null) {
			wrongLoginData = true;
			paint();
		} else {
			wrongLoginData = !main.checkLogin(usernameValue, passwordValue);
			paint();
		}
	}
}
